<template>
	<view>
		<my-search @click="gotoSearch"></my-search>
		<!-- 优化自定义组件的可用性: -->
		<!-- <my-search :bgcolor="'pink'" :radius="3"></my-search> -->
		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height: wh+'px'}" class="left-scroll-view">
				<!-- <view class="left-scroll-item active">左侧</view> -->
				<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scroll-item',i===active?'active':'']" @click="activeChanged(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height: wh+'px'}" class="right-scroll-view" :scroll-top="scrollTop">
				<view class="right-scroll-item" v-for="(item2,i2) in cateLevel2" :key="i2">
					<!-- 二级分类的标题 -->
					<view class="cate-lv2-title">
						<view class="cate-lv2-loading">
							<view class="loading">
								<span></span>
								<span></span>
								<span></span>
							</view>
						</view>
						<text>⊙ {{item2.cat_name}} ⊙</text>
					</view>
					<!-- 三级分类的区域 -->
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3"
							@click="gotoGoodsList(item3)">
							<!-- 图片 -->
							<image :src="item3.cat_icon.replace('dev','web')"></image>
							<!-- 文本 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 导入需要的mixins文件
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 当前设备可用的高度
				wh: 0,
				// 存储分类数据的方法:
				cateList: [],
				// 默认选中的样式
				active: 0,
				// 二级分类的数据
				cateLevel2: [],
				// 距离顶部的距离
				scrollTop: 0
			};
		},
		onLoad() {
			// 调用该方法可以调用一些设备的信息
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50
			// 调用分类列表数据的方法
			this.getCateList()
		},
		methods: {
			async getCateList() {
				// 发起请求
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				console.log(res)
				// 判断是否获取失败
				if (res.meta.status !== 200) return uni.$showMsg()
				// 转存数据
				this.cateList = res.message
				// 为二级分类赋值
				this.cateLevel2 = res.message[0].children
			},
			activeChanged(index) {
				this.active = index
				// 重新为二级分类赋值
				this.cateLevel2 = this.cateList[index].children
				// 让距离顶部的距离不断的变化,这样就可以回到原来的位置
				// this.scrollTop = this.scrollTop === 0 ? 1 : 0
				// 简化:
				this.scrollTop = this.scrollTop ? 0 : 1
			},
			// 三级分类跳转到商品页面详情处理函数
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			// 自定义搜索组件的点击事件
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;
	}

	.left-scroll-view {
		width: 240rpx;
	}

	.left-scroll-item {
		background-color: #f7f7f7;
		line-height: 112rpx;
		text-align: center;
		font-size: 24rpx;
		font-weight: 700;

		&.active {
			background-color: #FFFFFF;
			position: relative;
			transition: all linear 1s;

			&::before {
				content: '';
				display: block;
				width: 4rpx;
				height: 60rpx;
				background-color: #d9a8ff;
				position: absolute;
				top: 50%;
				left: 0;
				transform: translateY(-50%);
				// transition: all linear 1s;
			}
		}
	}

	.cate-lv2-title {
		// background-color: #d9a8ff;
		font-size: 30rpx;
		font-weight: 700;
		text-align: center;
		padding: 28rpx 0;
		position: relative;

		.cate-lv2-loading {
			position: absolute;
			top: 40rpx;
			left: 42rpx;

			.loading {
				--speed-of-animation: 0.9s;
				--gap: 6px;
				--first-color: #beadff;
				--second-color: #b8c9ff;
				--third-color: #ebc2ff;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 36rpx;
				gap: 6px;
				height: 20rpx;
			}

			.loading span {
				width: 8rpx;
				height: 40rpx;
				background: var(--first-color);
				animation: scale var(--speed-of-animation) ease-in-out infinite;
			}

			.loading span:nth-child(2) {
				background: var(--second-color);
				animation-delay: -0.8s;
			}

			.loading span:nth-child(3) {
				background: var(--third-color);
				animation-delay: -0.7s;
			}

			@keyframes scale {

				0%,
				40%,
				100% {
					transform: scaleY(0.1);
				}

				20% {
					transform: scaleY(1);
				}
			}
		}
	}

	.cate-lv3-list {
		display: flex;
		flex-wrap: wrap;

		.cate-lv3-item {
			width: 33.33%;
			display: flex;
			// 设置主轴的方向
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-bottom: 20rpx;

			image {
				width: 120rpx;
				height: 120rpx;
			}

			text {
				font-size: 24rpx;
				font-weight: 700;
			}
		}
	}
</style>
